from django.db import models
from django.contrib.auth.models import AbstractUser
from django.core.validators import RegexValidator
from django.contrib.auth.models import UserManager as _UserManager


class UserManager(_UserManager):
    def create_superuser(self, username, password, email=None,  **extra_fields):
        return super().create_superuser(username=username, email=email, password=password, **extra_fields)


# Create your models here.
class User(AbstractUser):
    objects = UserManager()

    # 最大长度是必须的
    mobile = models.CharField(max_length=11, verbose_name="手机号", unique=True, validators=[RegexValidator(r"^1[3-8]\d{9}$", message="手机号格式错误")], error_messages={
        "required": "手机号不能为空",
        "unique": "手机号已注册"
    })
    email_ac = models.BooleanField(default=False, verbose_name="邮箱状态")
    # 在这里面添加的字段，将会除了用户名密码之外，来创建用户
    REQUIRED_FIELDS = ["mobile", "email"]

    class Meta:
        db_table = "tb_user"
        verbose_name = "user"
        verbose_name_plural = "users"
